/*
 * Copyright (C) 2014 Félix Marín Ramírez | felixmurcia@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package calculadora.modelo;

/**
 *
 * @author Félix Marín Ramírez | felixmurcia@gmail.com
 */
public class ModeloPrimos {
    
    private int numInt;

    public ModeloPrimos() {
        numInt = -1;
    }  

    public int getNumInt() {
        return numInt;
    }

    public void setNumInt(int numInt) {
        this.numInt = numInt;
    }    

    public int[] primos(int n) {
        // Cálculo de los numeros primos entre 1 y MAX
        // utilizando el algoritmo de  Eratostenes 
        int i, j;
        final int MAX = 11111;
        boolean[] esPrimo = new boolean[MAX];
        int[] primos = new int[MAX];
        // Se inicializa la tabla
        esPrimo[0] = false;
        for (i = 0; i < MAX; i++) {
            esPrimo[i] = true;
        }
        // Empieza la criba 
        for (i = 2; i <= (MAX / 2); i++) {
            for (j = 2; j <= (MAX / i); j++) {
                esPrimo[i * j - 1] = false;
            }
        }
        // Se cuenta el número de primos encontrados
        for (i = 1; i <= n; i++) {
            if (esPrimo[i]) {
                numInt++;
            }
        }
        //System.out.println("Hasta el " + MAX + " hay " + j + " primos");
        int cont = 0;
        for (i = 1; i < MAX; i++) {
            if (esPrimo[i]) {
                //System.out.print(i + 1 + " ");
                primos[cont++] = i + 1;
            }
        }
        return primos;
    }
}
